/**
 * @param {number} x
 * @param {number} y
 * @param {number[][]} points
 * @return {number}
 */
var nearestValidPoint = function (x, y, points) {
  let index = points.length,
    result = Number.MAX_VALUE
  for (let i = 0; i < points.length; i++) {
    if (points[i][0] == x || points[i][1] == y) {
      const tmp = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1])
      if (tmp <= result) {
        result = Math.min(result, tmp)
        index = Math.min(index, i)
      }
    }
  }
  return flag? index:-1
}
